# -*- coding: UTF-8 -*-

import urllib2
import MultipartPostHandler
import threading

class Core(threading.Thread):
    stopthread = threading.Event()

    def __init__(self, applet, filename):
        threading.Thread.__init__(self)
        self.applet = applet
        self.filename = filename
        self.opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
        self.image_link = 0
        self.thumb_link = 0
        self.ad_link = 0
        self.image_name = 0
        
    def uploadFile(self, image):
        return None
        
    def response(self, data):
        return None
        
    def upload(self, image):
        data = self.uploadFile(image)
        self.response(data)

    def html_code(self):
        return "<a href=\"%s\" target=\"_blank\"><img src=\"%s\"></a>" % (self.image_link, self.thumb_link)
        
    def forum_code(self):
        return "[URL=%s][IMG]%s[/IMG][/URL]" % (self.image_link, self.thumb_link)
        
    def get_trumb(self):
        f = self.opener.open(self.thumb_link)
        data = f.read()
        f.close()
        return data
        
    def code(self, data):
        data = data.replace("%image%", self.image_link)
        data = data.replace("%ad%", self.ad_link)
        data = data.replace("%thumb%", self.thumb_link)
        data = data.replace("%filename%", self.image_name)
        return data
        
    def run(self):
        self.upload(self.filename)
        self.stopthread.set()
        self.applet.make(self)
        return True
